package com.mt.mtyx.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.mtyx.common.exception.BusinessException;
import com.mt.mtyx.common.result.Result;
import com.mt.mtyx.dto.product.SkuInfoDto;
import com.mt.mtyx.entity.product.SkuInfo;
import com.mt.mtyx.product.service.SkuAttrValueService;
import com.mt.mtyx.product.service.SkuImageService;
import com.mt.mtyx.product.service.SkuInfoService;
import com.mt.mtyx.product.service.SkuPosterService;
import com.mt.mtyx.vo.product.SkuInfoQueryVo;
import com.mt.mtyx.vo.product.SkuInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author WMT
 * @Date 2023/7/20 10:34
 */
@Api(tags = "商品信息")
@RequestMapping("/admin/product/skuInfo")
@RestController
public class SkuInfoController {

    @Autowired
    private SkuInfoService skuInfoService;

    @Autowired
    private SkuPosterService skuPosterService;

    @Autowired
    private SkuImageService skuImageService;

    @Autowired
    private SkuAttrValueService skuAttrValueService;

    @ApiOperation("分页条件查询SKU")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<SkuInfo>> selectAll(@PathVariable Long page,
                                            @PathVariable Long limit,
                                            SkuInfoQueryVo queryVo) {
        return Result.success(skuInfoService.selectAll(new Page<SkuInfo>(page, limit), queryVo));
    }

    @ApiOperation("获取SKU详情")
    @GetMapping("/get/{id}")
    public Result<SkuInfoVo> getDetails(@PathVariable Long id) {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        // 1.查询sku基本信息
        SkuInfoVo details = skuInfoService.getDetails(id);
        // 2.查询sku海报
        details.setSkuPosterList(skuPosterService.getPosterListBySkuId(id));

        // 3.查询sku图片
        details.setSkuImagesList(skuImageService.getImagesListBySkuId(id));

        // 4.查询sku平台属性
        details.setSkuAttrValueList(skuAttrValueService.getAttrValueListBySkuId(id));
        return Result.success(details);
    }


    @ApiOperation("添加SKU")
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody SkuInfoDto skuInfoDto) {
        if (StringUtils.isEmpty(skuInfoDto)) {
            throw new BusinessException("添加参数不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(skuInfoService.insert(skuInfoDto));
    }

    @ApiOperation("修改SKU")
    @PostMapping("/update")
    public Result<Integer> update(@RequestBody SkuInfoDto skuInfoDto) {
        if (StringUtils.isEmpty(skuInfoDto.getId())) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        if (StringUtils.isEmpty(skuInfoDto)) {
            throw new BusinessException("添加参数不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(skuInfoService.update(skuInfoDto));
    }


    @ApiOperation("商品审核")
    @GetMapping("/audit/{id}/{status}")
    public Result<Integer> audit(@PathVariable Long id,
                                 @PathVariable Integer status) {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(skuInfoService.audit(id, status));
    }

    @ApiOperation("商品上下架")
    @GetMapping("/publish/{id}/{status}")
    public Result<Integer> publish(@PathVariable Long id,
                                   @PathVariable Integer status) {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(skuInfoService.publish(id, status));
    }

    @ApiOperation("新人专享")
    @GetMapping("/isNewPerson/{id}/{status}")
    public Result<Integer> isNewPerson(@PathVariable Long id,
                                       @PathVariable Integer status) {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(skuInfoService.isNewPerson(id, status));
    }
}
